package vip.xumy.core.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**	All rights reserved
 *	author:mengyxu
 *	date:2019年6月26日
 */

@Component(value="springContextUtil")
public class SpringContextUtil implements ApplicationContextAware{
	private static ApplicationContext appContext;
	
	@Override
	public void setApplicationContext(ApplicationContext context)
			throws BeansException {
		SpringContextUtil.appContext = context;
	}
	
	@SuppressWarnings("unchecked")
	public static <T> T getBeanByName(String beanName){
		if(beanName == null){
			return null;
		}
		return (T) appContext.getBean(beanName);
	}

	public static <T> T getBeanByClass(Class<T> clazz){
		if(clazz == null){
			return null;
		}
		return (T) appContext.getBean(clazz);
	}
}
